package com.interview.employeeInfo.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.interview.checkinfo.entity.CheckInfo;
import com.interview.checkinfo.service.CheckInfoService;
import com.interview.core.attachment.entity.Attachment;
import com.interview.core.attachment.service.AttachmentService;
//
//import com.interview.core.loadfile.condition.LoadFilePath;
//import com.interview.core.loadfile.service.LoadFileServic;
import com.interview.core.page.Page;
import com.interview.employeeInfo.entity.EmployeeInfo;
import com.interview.employeeInfo.query.EmployeeInfoConditionQuery;
import com.interview.employeeInfo.service.EmployeeInfoService;

@Controller
@RequestMapping("employeeinfo")
public class EmployeeInfoController {
	@Resource
	protected EmployeeInfoService employeeInfoService;
	@Resource
	protected AttachmentService attachmentService;
	@Resource
	protected CheckInfoService checkInfoService;

	// @Resource
	// protected LoadFileServic loadFileServic;

	@RequestMapping(value = "{id}/view", method = RequestMethod.GET)
	public String view(@PathVariable String id, ModelMap model) {

		EmployeeInfo employeeInfo = employeeInfoService.findOne(id);

		model.put("employee", employeeInfo);
		return "employeeinfo/view";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String toAdd(ModelMap model) {
		return "employeeinfo/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(EmployeeInfo employee, ModelMap model) {

		String attachmentId = employee.getAttachmentId();

		if (attachmentId == "" || attachmentId.contains(" ")) {
			attachmentId = null;
			employee.setAttachmentId(null);
		}
		EmployeeInfo employeeInfo = employeeInfoService.save(employee);
		if (attachmentId != null) {
			Attachment attachment = attachmentService.findOne(attachmentId);
			attachment.setBizCode(employeeInfo.getId());
			attachmentService.update(attachment);
		}

		return "redirect:/employeeinfo/page";
	}

	@RequestMapping(value = "{id}/edit", method = RequestMethod.GET)
	public String toEdit(@PathVariable String id, ModelMap model) {

		EmployeeInfo employee = employeeInfoService.findOne(id);

		model.put("employee", employee);
		return "employeeinfo/edit";
	}

	@RequestMapping(value = "{id}", method = { RequestMethod.PUT })
	public String edit(EmployeeInfo employee, ModelMap model) {

		EmployeeInfo beforeEmployeeInfo = employeeInfoService.findOne(employee
				.getId());

		String attachmentId = employee.getAttachmentId();
		String beforeAttachmentId = beforeEmployeeInfo.getAttachmentId();

		if (beforeAttachmentId != null & attachmentId != null) {
			if (!attachmentId.equals(beforeAttachmentId)) {

				attachmentService.deleteFile(beforeAttachmentId);
			}
		}
		if (attachmentId == "" || attachmentId.contains(" ")) {
			attachmentId = null;
			employee.setAttachmentId(null);
		}
		if (attachmentId != null) {
			Attachment attachment = attachmentService.findOne(attachmentId);
			attachment.setBizCode(employee.getId());
			attachmentService.update(attachment);
		}

		employeeInfoService.update(employee);
		return "redirect:/employeeinfo/page";

	}

	@RequestMapping(value = "{id}", method = { RequestMethod.DELETE })
	public String delet(@PathVariable String id, ModelMap model) {

		EmployeeInfo employeeInfo = employeeInfoService.findOne(id);

		List<CheckInfo> checkInfoes = checkInfoService
				.dynamicQueryfindByEnployeeInfo(employeeInfo);

		if (checkInfoes.size() != 0) {
			for (CheckInfo checkInfo : checkInfoes) {
				checkInfoService.delet(checkInfo.getId());
			}
		}

		String attachmentId = employeeInfo.getAttachmentId();
		if (attachmentId != null) {
			if (attachmentService.findOne(attachmentId) != null) {
				attachmentService.deleteFile(attachmentId);
			}

		}
		employeeInfoService.delet(id);
		return "redirect:/employeeinfo/page";
	}

	@RequestMapping(value = "page", method = { RequestMethod.GET })
	public String page(ModelMap model, EmployeeInfoConditionQuery baseQuery) {

		Page page = employeeInfoService.dynamicQuery(baseQuery);

		List<EmployeeInfo> list = page.getList();
		Integer total = page.getTotal();
		Integer pageIndex = page.getPageIndex();
		Integer pageSize = page.getPageSize();

		Integer totalPage = (total / pageSize);

		if (total % pageSize != 0) {
			totalPage = totalPage + 1;
		}
		if (totalPage <= 0) {
			totalPage = 1;
		}
		;

		if (pageIndex >= totalPage & totalPage != 0) {
			pageIndex = totalPage - 1;
		}

		model.put("pageIndex", pageIndex);
		model.put("pageSize", pageSize);
		model.put("totalPage", totalPage);
		model.put("list", list);

		model.put("conditionQuery", baseQuery);

		return "employeeinfo/page";
	}

	@RequestMapping(value = "childpage", method = RequestMethod.GET)
	public String childPage(ModelMap map, EmployeeInfoConditionQuery baseQuery) {
		page(map, baseQuery);
		return "employeeinfo/childpage";
	}

	@RequestMapping(value = "{id}/childview", method = RequestMethod.GET)
	public String childView(@PathVariable String id, ModelMap model) {
		String relativeFilePath = null;
		EmployeeInfo employeeInfo = employeeInfoService.findOne(id);

		String attachmentId = employeeInfo.getAttachmentId();
		if (attachmentId != null) {
			Attachment attachment = attachmentService.findOne(attachmentId);

			relativeFilePath = attachment.getRelativeFilePath();

		}
		model.put("RelativePath", relativeFilePath);
		model.put("employee", employeeInfo);
		return "employeeinfo/childview";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));// true:允许输入空值，false:不能为空值
	}

	// json格式数据-----------------------------------------------------

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public Object empolyeeInfoData(@PathVariable String id, ModelMap model) {
		return employeeInfoService.findOne(id);
	}

	@RequestMapping()
	@ResponseBody
	public Object empolyeeInfoAllData(
			EmployeeInfoConditionQuery employeeInfoConditionQuery) {
		return employeeInfoService.dynamicQuery(employeeInfoConditionQuery);
	}

	// ==================================================================
	@RequestMapping(value = "jsessionid", method = RequestMethod.GET)
	public String jsessionid(ModelMap modelMap) {
		return "employeeinfo/jsessionid";
	}
}
